<?php
/***/
namespace app\admin\controller;

use app\admin\model\ErrorComparisonM;
use think\facade\Env;

/**
 * 错误对照管理
 * @Author    .guo
 * @DataTime  2019-05-17
 */
class ErrorComparison extends Father
{

	public function index()
	{
		if (input('get.api')) {

			$request = input('get.');

			$model   = new ErrorComparisonM;

			$data    = $model->getAllData($request);

			$count   = $data->total();

			$list    = $data->toArray();

			$list    = $list['data'];

			return json(['code'  => 0, 'msg'   => '', 'count' => $count, 'data'  => $list ]);

		} else {

			return $this->fetch();
		}
	}


	public function add()
	{
    	if(request()->isPost()) {

			$code = trim(input('post.code'));
			$desc = trim(input('post.desc'));

			$data = ['code' => $code, 'desc' => $desc ];

			$validate = validate('ErrorComparisonV');
			if(!$validate->scene('add')->check($data)) {

			    echo $validate->getError(); exit;
			}

			//处理数据
			$model  = new ErrorComparisonM;
			$result = $model->addOne($data);

			if ($result) {

				echo 'success';
			} else {

				echo "服务器繁忙";
			}

			return;

    	} else {

			return $this->fetch();
    	}
	}

	public function edit()
	{
    	if(request()->isPost()) {

			$code = trim(input('post.code'));
			$desc = trim(input('post.desc'));
			$id   = trim(input('post.id'));

			$data = ['code' => $code, 'desc' => $desc , 'id' => $id ];

			$validate = validate('ErrorComparisonV');
			if(!$validate->scene('edit')->check($data)) {

			    echo $validate->getError(); exit;
			}

			//处理数据
			$model  = new ErrorComparisonM;
			$result = $model->updateOne($data);

			if ($result !== false) {

				echo 'success';
			} else {

				echo "服务器繁忙";
			}

			return;

    	} else {

    		$data = input('get.');

    		$validate = validate('ErrorComparisonV');
    		if(!$validate->scene('getid')->check($data)) {

    		    echo $validate->getError(); exit;
    		}

    		$info = ErrorComparisonM::get($data['id']);
    		$this->assign('info' ,$info);

			return $this->fetch();
    	}
	}

	public function del()
	{
    	if(request()->isPost()) {

			$data = input('post.');

			$validate = validate('ErrorComparisonV');
			if(!$validate->scene('getid')->check($data)) {

			    echo $validate->getError(); exit;
			}

			//处理数据
			$model  = new ErrorComparisonM;
			$result = $model->delOne($data);

			if ($result !== false) {

				echo 'success';
			} else {

				echo "服务器繁忙";
			}

			return;

    	}
	}

	//导出
	public function listExport()
	{
		require_once Env::get('vendor_path').'PHPExcel/PHPExcel.php';

		$obj = new \PHPExcel();

		$obj->getProperties()->setTitle('对照表'); //设置标题
		// 设置当前sheet
        $obj->setActiveSheetIndex(0);
        // 设置当前sheet的名称
        $obj->getActiveSheet()->setTitle('对照表');
        // 列标
        $list = ['A', 'B'];
        // 填充第一行数据
        $obj->getActiveSheet()
            ->setCellValue($list[0] . '1', '错误码')
            ->setCellValue($list[1] . '1', '描述');

        $model   = new ErrorComparisonM;

		$data    = $model->select();

        $length = count($data);
        // 左对齐
        foreach ($list as $col) {
            // 设置第1-n行，左对齐
            for ($i = 1; $i <= $length + 1; $i++) {
                $obj->getActiveSheet()->getStyle($col . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
            }
        }

        // 填充第n(n>=2, n∈N*)行数据
        for ($i = 0; $i < $length; $i++) {
            $obj->getActiveSheet()->setCellValue($list[0] . ($i + 2), $data[$i]['code'], \PHPExcel_Cell_DataType::TYPE_STRING);//将其设置为文本格式
            $obj->getActiveSheet()->setCellValue($list[1] . ($i + 2), $data[$i]['desc']);
        }
        // 设置列宽
        $obj->getActiveSheet()->getColumnDimension('A')->setWidth(25);
        $obj->getActiveSheet()->getColumnDimension('B')->setWidth(65);

        // 导出
        ob_clean();

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="错误对照表.xlsx"');
        header('Cache-Control: max-age=1');
        $objWriter = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
        $objWriter->save('php://output');
        exit;
	}

}
